<?php

namespace app\admin\controller;

use app\admin\model\Node;
use app\admin\controller\Base;
use app\admin\model\Role_node;
use think\Request;
use think\Db;

class Nodes extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $rel = Db::name('node')->paginate(6,false,[
            'type'     => 'Bootstrap',
            'var_page' => 'page',
            'path'=>'javascript:AjaxPage([PAGE]);',
            //使用函数助手传入参数
            'query' => request()->param()
        ]);
        return view('node/index', ['data'=>$rel]);
    }

    public function ajaxListAction(){

        $page = request()->param('apage');

        if (!empty($page)) {
            $rel = model('node')->paginate(6,false,[
                'type'=> 'Bootstrap',
                'var_page' => 'page',
                'page' => $page,
                'path'=>'javascript:AjaxPage([PAGE]);'

            ]);
            $page = $rel->render();
        }
        return json(['data'=>$rel,'page'=>$page]);

    }

    public function ajaxListSearch()
    {
        $name = input('post.')['name'];
        $data = Node::where('name', 'like', $name.'%')->paginate(6,false,[
            'type'     => 'Bootstrap',
            'var_page' => 'page',
            'path'=>'javascript:AjaxPage([PAGE]);',
            //使用函数助手传入参数
            'query' => request()->param()
        ]);

        return json(['data'=>$data]);
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('node/add');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $info = input('post.');
        $result = Node::create($info)->getData();
        $data = [];
        if ($result['id']>0){
            $data['status'] = true;
            $data['message'] = '添加成功';
        }else{
            $data['status'] = false;
            $data['message'] = '添加失败';
        }
        return json($data);
    }





    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = input('put.');
        $node = Node::get($id);

        $node->controller = $data['controller'];
        $node->method = $data['method'];
        $node->name = $data['name'];
        $node->status = $data['status'];
        $result = $node->save();
        $data = [];
        if ($result>0){
            $data['status'] = true;
            $data['message'] = '添加成功';
        }else{
            $data['status'] = false;
            $data['message'] = '添加失败';
        }
        return json($data);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $role =  Node::get($id);
        $result = $role->delete();
        Role_node::where('nid', '=', $id)->delete();
        $data = [];
        if ($result>0){
            $data['status'] = true;
            $data['message'] = '删除成功';
        }else{
            $data['status'] = false;
            $data['message'] = '删除失败';
        }
        return json($data);
    }

}
